﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace MonitorPlan.devices
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if(!IsPostBack)
            {
                string hi = "";
                // redirect if user does not login
                if (Session["ClientId"] == null)
                {
                    Response.Redirect("/");
                }
                else
                {
                    hi = Session["ClientId"].ToString();
                }

                // bind house to dropdownlist
                sd1.SelectCommand = "select houseid, housename from house where userid=" + hi + " and isdeleted=0";
                ddlHouse.DataBind();
                //if (ddlHouse.Items.Count > 0)
                //{
                //    ddlHouse.SelectedIndex = 0;
                //}
                // get device type for each house
                if (ddlHouse.Text != "")
                {
                    // get device types for the selected house
                    string HouseId = ddlHouse.SelectedValue.ToString();
                    Model.ToolType tt = new Model.ToolType();
                    List<string> tl = tt.GetToolTypes(HouseId);
                    tl.Sort();
                    report.InnerHtml = "";
                    // loop through all device type
                    for (int i = 0; i < tl.Count; i++)
                    {
                        report.InnerHtml += "<h4 class='text-info'><u>" + tl[i] + "</u></h4>";

                        // get tools for the current tool types
                        DataTable dt = tt.GetTools(HouseId, tl[i]);

                        // loop to get tool name in each type
                        report.InnerHtml += "<ul class='device'>";
                        foreach (DataRow dr in dt.Rows)
                        {
                            string Status = new Model.ToolType().getLastTrans(HouseId, dr["toolid"].ToString());
                            // if no transaction
                            if (Status == "")
                                Status = "lightoff.png";
                            report.InnerHtml += "<li><img src='/img/" + Status + "' width='22' height='20' /> " + dr["toolname"].ToString() + "</li>";
                        }
                        report.InnerHtml += "</ul>";
                    }
                }
            }
        }

        protected void ddlHouse_SelectedIndexChanged(object sender, EventArgs e)
        { 
            
            if (ddlHouse.Text != "")
            {
                // get device types for the selected house
                string HouseId = ddlHouse.SelectedValue.ToString();
                Model.ToolType tt = new Model.ToolType();
                List<string> tl = tt.GetToolTypes(HouseId);
                
                tl.Sort();
                report.InnerHtml = "";
                // loop through all device type
                for (int i = 0; i < tl.Count; i++)
                {
                    report.InnerHtml += "<h4 class='text-info'><u>" + tl[i] + "</u></h4>";
                    // get tools for the current tool types
                    DataTable dt = tt.GetTools(HouseId, tl[i]);
                    report.InnerHtml += "<ul class='device'>";
                    // loop to get tool name in each type
                    foreach(DataRow dr in dt.Rows)
                    {
                        string Status = new Model.ToolType().getLastTrans(HouseId, dr["toolid"].ToString());
                        // if no transaction
                        if (Status == "")
                            Status = "lightoff.png";
                        report.InnerHtml += "<li><img src='/img/" + Status + "' width='22' height='20' /> " + dr["toolname"].ToString() + "</li>";
                    }
                    report.InnerHtml += "</ul>";
                }
                // set the selected index

            }
        }
    }
}